package com.yogee.topseller.activity.my;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.AppManager;
import com.yogee.core.utils.LogUtils;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.MainActivity;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.login.model.bean.SMSbean;
import com.yogee.topseller.activity.login.presenter.LoginPresenter;
import com.yogee.topseller.activity.login.view.ILoginView;
import com.yogee.topseller.activity.login.view.impl.LoginActivity;
import com.yogee.topseller.bean.RemoveWeChatBean;
import com.yogee.topseller.bean.RetunContactBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.view.AuthortyDialog;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class WeChatAuthorization extends HttpActivity {
    
    Unbinder unbinder;
    @BindView(R.id.title_white_left_back)
    LinearLayout titleWhiteLeftBack;
    @BindView(R.id.tv_authorty_text)
    TextView tvAuthortyText;
    @BindView(R.id.ll_we_chat)
    LinearLayout llWeChat;
    @BindView(R.id.title_white_title)
    TextView titleWhiteTitle;

    private boolean isAuthory = false;//是否授权标识
    private UMShareAPI mShareAPI = null;
    private String openid;
    private String headerUrl;
    private String name;
    private String sex;
    private String unionid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        unbinder = ButterKnife.bind(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wechat_authorization;
    }

    @Override
    protected void initView() {
        titleWhiteTitle.setText("微信授权");
        mShareAPI = UMShareAPI.get(this);
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        UMShareAPI.get(this).setShareConfig(config);
        if (null != getIntent()){
            String tempAuthory = getIntent().getStringExtra("isAuthory");
            if ("1".equals(tempAuthory)){
                isAuthory = true;
            }else{
                isAuthory = false;
            }
        }
        loadAuthorty();
    }

    private void loadAuthorty() {
        if (isAuthory){
            tvAuthortyText.setText("已授权");
            tvAuthortyText.setTextColor(getResources().getColor(R.color.select_color));
        }else{
            tvAuthortyText.setText("未授权");
            tvAuthortyText.setTextColor(getResources().getColor(R.color.hint_color1));
        }
    }

    @OnClick({R.id.title_white_left_back, R.id.ll_we_chat})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_white_left_back:
                if (AppUtil.isFastDoubleClick(500)) {
                    return;
                }
                finish();
                break;
            case R.id.ll_we_chat:
                if (isAuthory){
                    AuthortyDialog.Builder builder = new AuthortyDialog.Builder(WeChatAuthorization.this);
                    builder.setTitle("您是否要解除授权？").setMessage("解除后将不能使用微信方式登录")
                            .setPositiveButton("解除授权", new AlertDialog.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    unbindAuthorty();
                                    dialog.dismiss();
                                }
                            })
                            .setNegativeButton("取消", new AlertDialog.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    builder.create().show();
                }else{
                    //未授权逻辑
                    mShareAPI.getPlatformInfo(this, SHARE_MEDIA.WEIXIN, umAuthListener);
                }
                break;
        }
    }

    private void unbindAuthorty(){
        HttpManager.getInstance().removeWechat(AppUtil.getUserId(WeChatAuthorization.this)).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<RemoveWeChatBean>() {
            @Override
            public void onNext(RemoveWeChatBean o) {
                if ("1".equals(o.getResult())) {
                    if ("0".equals(o.getIsAuthorization())){
                        isAuthory = false;
                    }else{
                        isAuthory = true;
                    }
                    loadAuthorty();
                }
            }
        }, this));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //授权开始的回调
            LogUtils.e("test", "onStart");
        }

        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {

            LogUtils.e("test", "onComplete");
            openid = data.get("openid");
            unionid = data.get("unionid");
            headerUrl = data.get("iconurl");
            name = data.get("name");
            sex = "男".equals(data.get("gender")) ? "1" : "0";
            changeWechat();
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            LogUtils.e("test", "onError");
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            LogUtils.e("test", "onCancel");
        }
    };

    private void changeWechat(){
        HttpManager.getInstance().changeWechat(openid,unionid,AppUtil.getUserId(WeChatAuthorization.this)).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<RemoveWeChatBean>() {
            @Override
            public void onNext(RemoveWeChatBean o) {
                if ("1".equals(o.getResult())) {
                    if ("0".equals(o.getIsAuthorization())){
                        isAuthory = false;
                    }else{
                        isAuthory = true;
                    }
                    loadAuthorty();
                }
            }
        }, this));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.e("test", "onActivityResult");
        mShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

}
